package com.disney.dolcn.invitemail.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * class KeyWordsConstant defines sensitive words collection.
 * 
 * @author hlwu
 * @version Revision:1.0
 */
public final class KeyWordsConstant {
	private static List<String> keysets = null;
	private static String sec = ",";

	/**
	 * Private Constructor.
	 */
	private KeyWordsConstant() {

	}

	/**
	 * Initialize key words file.
	 */
	private static synchronized void initKeySets(Properties app) {
		if(keysets==null){
			keysets = new ArrayList<String>();
			InputStream in = null;
			try {
				in = new FileInputStream(new File(app
						.getProperty(InviteConstant.KEYWORDSURL)));

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(in));
				String str = reader.readLine();
				while (str != null) {
					String[] keys = (str.trim()).split(sec);
					if (keys != null) {
						for (int i = 0; i < keys.length; i++) {
							keysets.add(keys[i]);
						}
					}
					str = reader.readLine();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * Get sensitive words collection.
	 * 
	 * @return {@link List}
	 */
	public static List<String> getKeyWords(Properties app) {
		if (keysets == null) {
			initKeySets(app);
		}
		return keysets;
	}
}
